﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using SubSonic.Extensions;
using Eacademy.Domain.Repository;
using Eacademy.Domain.Abstractions;
using Eacademy.Domain.Models;
using Eacademy.Library.Helpers;
using Ninject;

namespace Eacademy.Library.Membership
{
    public class CustomRoleProvider : RoleProvider
    {

        //Ninject bindings
        private IUserRepository _userRepository;

        [Inject]
        public IUserRepository UserRepository
        {
            set
            {
                _userRepository = value;
            }
        }

        //private IProfileRepository _profileRepository;

        //[Inject]
        //public IProfileRepository ProfileRepository
        //{
        //    set
        //    {
        //        _profileRepository = value;
        //    }
        //}

        public CustomRoleProvider()
        {
            MvcApplication.Container.Inject(this);
        }

        #region Unimplemented RoleProvider Methods

        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override string ApplicationName { get; set; }

        public override void CreateRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }

        public override string[] GetAllRoles()
        {
            throw new NotImplementedException();
        }

        public override string[] GetUsersInRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override bool RoleExists(string roleName)
        {
            throw new NotImplementedException();
        }

        #endregion

        public override string[] GetRolesForUser(string username)
        {
            User user = _userRepository.GetUserByUsername(username);

            List<string> userType = new List<string>();

            user.UserType = UserHelper.GetUserTypeById(user.UserTypeId);

            userType.Add(user.UserType.ToPascalCase());

            string[] roles = userType.ToArray();

            return roles;
        }


        public override bool IsUserInRole(string username, string roleName)
        {
            User user = _userRepository.GetUserByUsername(username);
            string userRole = String.Empty;
            if (user != null)
            {

                userRole = UserHelper.GetUserTypeById(user.UserTypeId).ToPascalCase();

                if (userRole == roleName)
                {
                    return true;
                }
            }
            return false;
        }


    }
}